package com.logos.musicframe.controller;

import com.logos.musicframe.resp.SongsResp;
import com.logos.musicframe.result.Result;
import com.logos.musicframe.service.SongsLikedService;
import com.logos.musicframe.utils.LocalSessionStorageUtil;
import com.logos.musicframe.utils.UserInfoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 点赞行为记录表(SongsLiked)表控制层
 *
 * @author rose
 * @since 2025-06-02 22:58:23
 */
@Component
@Slf4j
public class SongsLikedController {
    /**
     * 服务对象
     */
    @Autowired
    private SongsLikedService songsLikedService;

    @Autowired
    private UserInfoUtil userInfoUtil;


    /**
     * 获取当前用户喜欢歌曲列表
     */
    public Result<List<SongsResp>> getUserLikedSongs() {
        // 拿到当前用户的登录态
        String[] loginSession = LocalSessionStorageUtil.getLoginSession();
        if (loginSession == null) {
            log.info("用户未登录");
            return Result.fail("用户未登录");
        }
        String userId = loginSession[0];
        log.info("用户id:{}", userId);
        List<SongsResp> result = songsLikedService.getUserLikedSongs(userInfoUtil.getUserId());
        return Result.ok(result);
    }
    
    /**
     * 喜欢歌曲
     * @param songId 歌曲ID
     * @return 操作结果
     */
    public Result<Boolean> like(Integer songId) {
        try {
            // 拿到当前用户的登录态
            Integer userId = userInfoUtil.getUserInfo().getId();
            boolean result = songsLikedService.likeSong(userId, songId);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("SongsLikedController.like.error{}", e);
            return Result.fail("喜欢歌曲失败");
        }
    }
    
    /**
     * 取消喜欢歌曲
     * @param songId 歌曲ID
     * @return 操作结果
     */
    public Result<Boolean> unlike(Integer songId) {
        try {
            // 拿到当前用户的登录态
            Integer userId = userInfoUtil.getUserInfo().getId();
            boolean result = songsLikedService.unlikeSong(userId, songId);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("SongsLikedController.unlike", e);
            return Result.fail("取消喜欢歌曲失败");
        }
    }
}

